<?php

namespace app\controller;

use app\BaseController;
use app\controller\model\apis\Hi;
use app\controller\model\apis\service\Bochuang;
use app\controller\model\Config;
use app\controller\model\SPay;
use app\controller\model\Tables;
use think\facade\Session;
use think\facade\Db;
use yeepay\Util\Base64Url;

class YeePayNotify extends BaseController
{
	protected $d,$toName,$model,$tables,$orderNo,$order,$refundResult,$data;
	public function __construct()
	{
		$this->tables = new Tables();
	}

	public function pushPay(){
		$post = request()->param();
		$file=fopen("./t_yeepay.txt","w");
		fwrite($file,json_encode($post));
		fclose($file);

		$service = DB::name($this->tables->service)->where("id",2)->find();
		$model = new Bochuang($service);
		$re = $model->setSuccess($post);
		if($re){
			//开始处理结算业务成功
			$summaryNo = $re['summaryNo'];//结算单号
			$yopMerchantNo = $re['yopMerchantNo'];//商户号
			$settleAmount = $re['settleAmount'];
		}
		echojson([],200,'');
	}
	
	public function pay()
    {
        $param = request()->param();
        $log = new \app\Log('yeePayNotify/pay');
        $log->write(['param' => $param]);
        $data = $param['response'];
        [$aesBase,$pubDat,$aes,$sha] = explode("$",$data);
        $cert = file_get_contents("./4958323926.pfx");
        openssl_pkcs12_read($cert, $dta, "");
        ($dta) or die('密钥不可用');
        $privateKey = $dta['pkey'];
        $privateKey = openssl_pkey_get_private($privateKey);
        openssl_private_decrypt(Base64Url::decode($aesBase),$des,$privateKey);
        //aes解密参数
        $aesData = openssl_decrypt(Base64Url::decode($pubDat),"AES-128-ECB",$des,OPENSSL_RAW_DATA);
        $data = explode("$",$aesData);

        $data = json_decode($data[0],true);
        $data['settleRecordDetailsDtos'] = json_decode($data['settleRecordDetailsDtos'], true);
        $log->write(['data' => $data]);
        // return json($data);
        exit("SUCCESS");
    }
    
    public function autoPay()
    {
        $param = request()->param();
        $pay_succ = (new model\apis\service\YeepayModel($param['id']))->getPayMoney($param['shopNo']);
        echojson($pay_succ);
    }
}
